package com.yida.file.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.file.service.IMinioSysFileService;
import com.yida.system.api.domain.SysFile;
import com.yida.system.api.domain.UploadRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件请求处理
 *
 * @author yida
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件系统")
public class SysFileController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private IMinioSysFileService sysFileService;
    // @Autowired
    // private ISysFileService sysFileService;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            // return R.ok(sysFile);
            return R.ok(sysFile, "文件上传成功！");
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("文件下载")
    @PostMapping("download")
    public R<SysFile> download(MultipartFile file) {
        try {
            // 上传并返回访问地址
            // String url = sysFileService.download(file);
            // String url = sysFileService.download(file);
            String url = null;
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            // return R.ok(sysFile);
            return R.ok(sysFile, "文件上传成功！");
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * ===============================================minio分布式存储=====================================================
     */

    @ApiOperation("minio文件上传")
    @PostMapping("/minioUpload")
    public AjaxResult upload(@RequestParam String bucketName, @RequestParam(required = false) List<String> fileNames, @RequestPart(value = "files") MultipartFile[] files) throws Exception {
        return AjaxResult.success(sysFileService.uploadInBucket(bucketName, fileNames, files));
    }

    @ApiOperation("文件动态链接")
    @GetMapping("/dynamicLink")
    public AjaxResult listUrls(UploadRequest request) {
        return AjaxResult.success(sysFileService.dynamicLink(request));
    }

    // ---------------------------------------新增功能区--------------------------------------------
    // 文件上传接口动态地址返回
    @PostMapping("/uploadFileDynamicLink")
    public R<SysFile> uploadFileDynamicLink(MultipartFile file, @RequestParam String bucketName, @RequestParam(required = false) String fileName, @RequestParam boolean dynamicLinkFlag) throws Exception {
        try {
            SysFile sysFile = sysFileService.uploadFileDynamicLink(file, bucketName, fileName, dynamicLinkFlag);
            return R.ok(sysFile, "uploadFileDynamicLink文件上传成功！");
        } catch (Exception e) {
            log.error("uploadFileDynamicLink上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    // 获取文件信息
    @PostMapping("/getFileInfo")
    public R<SysFile> getFileInfo(@RequestBody UploadRequest request) {
        SysFile sysFile = sysFileService.getFileInfo(request);
        if (StringUtils.isNotNull(sysFile)) {
            return R.ok(sysFile, "查询成功！");
        }
        return R.fail("获取文件信息失败！");
    }

    // 获取文件组信息
    @PostMapping("/getFileInfoList")
    public R<List<SysFile>> getFileInfoList(@RequestBody UploadRequest request) {
        List<SysFile> list = sysFileService.getFileInfoList(request);
        if (StringUtils.isNotEmpty(list)) {
            return R.ok(list, "查询成功！");
        }
        return R.fail("获取文件组信息失败！");
    }

    // 删除文件
    @DeleteMapping("/delFile")
    public R<Boolean> delFile(@RequestBody UploadRequest request) {
        if (sysFileService.delFile(request)) {
            return R.ok(true);
        }
        return R.fail(false);
    }

}